﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using mmoss.orm;
using mmoss.application.classes;
using mmoss.configuration;
using System.Windows.Forms;

namespace mmoss.application
{
    public partial class frmItemAddOneShot : Form
    {
        public InventoryItemType ItemType { get; set; }
        public InventoryItemByVendorMMC ItemCreated { get; set; }
        public frmItemAddOneShot()
        {
            InitializeComponent();
            this.Load+=new EventHandler(frmItemAddOneShot_Load);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //create both item and vendoritem
            InventoryItem l_item = new InventoryItem();
            l_item.InventoryItemType = (InventoryItemType) cboType.SelectedItem;
            l_item.itemShortDesc = txtItemName.Text;
            l_item.itemLongDesc = txtItemName.Text;
            l_item.User = Default.CurrentUser;
            InventoryItemByVendorMMC l_itemMMC = new InventoryItemByVendorMMC();
            l_itemMMC.Vendor = (Vendor) cboVendor.SelectedItem;
            //set initial inventory amount
            InventoryAdjustmentMMC l_adjustment = new InventoryAdjustmentMMC();
            l_adjustment.InventoryItemByVendorMMC = l_itemMMC;
            l_adjustment.Oldquant = 0;
            l_adjustment.Newquant = decimal.Parse(txtInitialQuant.Text);
            l_adjustment.User = Default.CurrentUser;

            //save item
            dbConn.db.SubmitChanges();

            //close form
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //close form without creating item
            this.Close();
        }

        private void frmItemAddOneShot_Load(object sender, EventArgs e)
        {
            //load pulldowns
            cboVendor.DataSource = dbConn.db.Vendors;
            cboVendor.DisplayMember = "vendorName";
            cboVendor.ValueMember = "vendorID";

            cboType.DataSource = dbConn.db.InventoryItemTypes;
            cboType.DisplayMember = "InventoryTypeDesc";
            cboType.ValueMember = "InventoryTypeID";

            cboCostTable.DataSource = dbConn.db.InventoryPriceTables;
            cboCostTable.DisplayMember = "priceTableName";
            cboCostTable.ValueMember = "priceTableID";

            cboPriceTable.DataSource = dbConn.db.InventoryPriceTables;
            cboPriceTable.DisplayMember = "priceTableName";
            cboPriceTable.ValueMember = "priceTableID";
            

            //check on type
            if (ItemType != null)
            {
                if (cboType.FindStringExact(ItemType.InventoryTypeDesc) != -1)
                {
                    cboType.SelectedIndex = cboType.FindStringExact(ItemType.InventoryTypeDesc);
                    //set the default
                    cboType.Enabled = false;
                }
            }
        }


    }
}
